package com.lq.ecoldchain.ui.main.orderlist.orderover

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lq.ecoldchain.R
import com.lq.ecoldchain.base.BaseNavigator
import com.lq.ecoldchain.base.view.BaseActivity
import com.lq.ecoldchain.callback.SafeClickListener
import com.lq.ecoldchain.databinding.ActivityOrderOverBinding
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.*
import kotlinx.android.synthetic.main.activity_order_over.*
import kotlinx.android.synthetic.main.activity_verified.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance

/*
*2019/1/17 0017  19:27
*function by lq
*/
@Route(path =BaseNavigator.orderOver)
class OrderOverActivity:BaseActivity<ActivityOrderOverBinding>(){
    override val layoutId = R.layout.activity_order_over
    override val viewDelegate : OrderOverDelegate by instance()
    private var receipt :String?=""//收货单图片地址
    private var temperatureController:String?=""//温控仪图片地址
    private var oilCard:String?=""//油卡图片地址

    private  val RECODE =1
    private val TECODE = 2
    private val OICODE = 3
    private var currentCode =4

    @Autowired
    @JvmField
    var id:String?=""

    override fun initView() {
        ARouter.getInstance().inject(this)
        titleText("配送完成")
        binding.delegate = viewDelegate
        orderOverTv.setOnClickListener { orderOver() }
        initListener()
    }

    private fun initListener(){
        orderOver1.setOnClickListener(object:SafeClickListener(){
            override fun onNoDoubleClick(v: View) {
                currentCode = RECODE
                PhotoShowUtils.choosePhoto(v)
            }
        })
        orderOver2.setOnClickListener(object:SafeClickListener(){
            override fun onNoDoubleClick(v: View) {
                currentCode = TECODE
                PhotoShowUtils.choosePhoto(v)
            }
        })
        orderOver3.setOnClickListener(object:SafeClickListener(){
            override fun onNoDoubleClick(v: View) {
                currentCode = OICODE
                PhotoShowUtils.choosePhoto(v)
            }
        })
    }

    private fun orderOver(){
        if(receipt.isNullOrEmpty()  || temperatureController.isNullOrEmpty()||oilCard.isNullOrEmpty()){
            toast { "请上传所有图片信息" }
        }else{
            BaseNetCore.ApiServiceImpl.orderOver(id!!,receipt!!,temperatureController!!,oilCard!!).compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<String>(){
                override fun onSuccess(t: String?) {
                    toast { "配送完成" }
                    finish()
                }

                override fun onFail(code: String) {}
            })
        }
    }

    override val kodein = Kodein.lazy {
        extend(parentKodein)
        import(orderOverKodeinModule)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode==RESULT_OK){
            when(currentCode){
               RECODE->{
                   CameraUtil.handlePhoto(requestCode, data?.data, orderOver1, object : QiNiuUtils.OnQiNiuListener {
                       override fun uploadSuccess(url: String) {
                          receipt = url
                       }
                   })
               }
                TECODE ->{
                    CameraUtil.handlePhoto(requestCode, data?.data, orderOver2, object : QiNiuUtils.OnQiNiuListener {
                        override fun uploadSuccess(url: String) {
                            temperatureController = url
                        }
                    })
                }
                OICODE->{
                    CameraUtil.handlePhoto(requestCode, data?.data, orderOver3, object : QiNiuUtils.OnQiNiuListener {
                        override fun uploadSuccess(url: String) {
                            oilCard = url
                        }
                    })
                }
            }
        }
    }

}